package com.yijumao.controller;


import com.yijumao.entity.User;
import com.yijumao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public String add(User user){
        userService.add(user);
        return "redirect:/user/findAll";
    }

    @GetMapping("/toAdd")
    public String toAdd(){
        return "user-add";
    }

    @GetMapping("/findAll")
    public String findAll(Model model){
        List<User> users = userService.findAll();
        model.addAttribute("users",users);
        return "user-list";
    }

    @GetMapping("/toUpdate")
    public String toUpdate(Model model,Integer uid){
        User user = userService.findById(uid);
        model.addAttribute("user",user);
        return "user-update";
    }

    @PostMapping("/update")
    public String update(User user){
        userService.update(user);
        return "redirect:/user/findAll";
    }

    @GetMapping("/delete")
    public String delete(Integer uid){
        userService.delete(uid);
        return "redirect:/user/findAll";
    }

}

